package net.sk.china.platform.service.impl.system;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import net.sk.china.platform.constants.Constants;
import net.sk.china.platform.dao.system.SystemAttachmentDao;
import net.sk.china.platform.model.system.SystemAttachment;
import net.sk.china.platform.request.system.SystemAttachmentMoveRequest;
import net.sk.china.platform.request.system.SystemAttachmentRequest;
import net.sk.china.platform.service.system.SystemAttachmentService;
import net.sk.china.platform.service.system.SystemConfigService;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.utils.TalkUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * SystemAttachmentServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/3 16:04
 * // +----------------------------------------------------------------------
 */
@Service
public class SystemAttachmentServiceImpl extends ServiceImpl<SystemAttachmentDao, SystemAttachment> implements SystemAttachmentService {

    @Resource
    private SystemAttachmentDao dao;

    private final SystemConfigService systemConfigService;

    public SystemAttachmentServiceImpl(SystemConfigService systemConfigService) {
        this.systemConfigService = systemConfigService;
    }

    /**
     * 给图片加前缀
     * @param path String 路径
     * @return {@code String}
     */
    @Override
    public String prefixImage(String path) {
        return path.replace(Constants.UPLOAD_TYPE_IMAGE + "/", getCdnUrl() + Constants.UPLOAD_TYPE_IMAGE+"/");
    }

    /**
     * 同步到云服务， 更新图片上传类型
     * @param attId Integer 主键id
     * @param type int 图片上传类型 1本地 2七牛云 3OSS 4COS
     */
    @Override
    public void updateCloudType(Integer attId, int type) {
        SystemAttachment systemAttachment = new SystemAttachment();
        systemAttachment.setAttId(attId);
        systemAttachment.setImageType(type);
        updateById(systemAttachment);
    }

    /**
     * 附件分页
     * @param map 查询条件
     * @param pageParamRequest PageParamRequest 分页参数
     * @return {@code List<SystemAttachment>}
     */
    @Override
    public List<SystemAttachment> getList(Map<String, Object> map, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        Integer uid = (Integer) map.get("uid");
        Integer pid = (Integer) map.get("pid");
        // 格式png,jpeg,jpg,audio/mpeg,text/plain,video/mp4,gif
        String attType = (String) map.get("attType");

        LambdaQueryWrapper<SystemAttachment> query = new LambdaQueryWrapper<>();
        query.eq(pid != 0, SystemAttachment::getPid, pid);
        query.eq(ObjectUtil.isNotNull(uid), SystemAttachment::getUid, uid);
//        if(StringUtils.isNotEmpty(attType)){
//            Object[] string = StringUtils.split(attType,",");
//            query.in(SystemAttachment::getAttType, string);
//        }
        query.orderByDesc(SystemAttachment::getAttId);
        return dao.selectList(query);
    }

    /**
     * 新增附件
     * @param systemAttachmentRequest 新增参数
     * @return {@code boolean}
     */
    @Override
    public boolean add(SystemAttachmentRequest systemAttachmentRequest) {
        SystemAttachment systemAttachment = new SystemAttachment();
        BeanUtils.copyProperties(systemAttachmentRequest, systemAttachment);
        return save(systemAttachment);
    }

    /**
     * 编辑附件
     * @param systemAttachmentRequest 更新参数
     * @return {@code boolean}
     */
    @Override
    public boolean edit(SystemAttachmentRequest systemAttachmentRequest) {
        SystemAttachment systemAttachment = new SystemAttachment();
        BeanUtils.copyProperties(systemAttachmentRequest, systemAttachment);
        return updateById(systemAttachment);
    }

    /**
     * 更改图片目录
     * @param move 参数
     * @return {@code boolean}
     */
    @Override
    public boolean updateAttrId(SystemAttachmentMoveRequest move) {
        LambdaUpdateWrapper<SystemAttachment> query = new LambdaUpdateWrapper<>();
        query.in(SystemAttachment::getAttId, TalkUtil.stringToArray(move.getAttrId()));
        query.set(SystemAttachment::getPid, move.getPid());
        return update(query);
    }

    /**
     * 清除 cdn url， 在保存数据的时候使用
     * @param path String 文件路径
     * @return {@code String}
     */
    @Override
    public String clearPrefix(String path) {
        if(StringUtils.isBlank(path)){
            return path;
        }
        Constants.CND_URL = getCdnUrl();
        if(path.contains(getCdnUrl() + "/")){
            return path.replace(getCdnUrl() + "/", "");
        }

        return path;
    }

    /**
     * 获取cdn url
     * @return {@code String}
     */
    private String getCdnUrl() {
        String uploadType = systemConfigService.getValueByKeyException("uploadType");
        // 获取配置信息
        int type = Integer.parseInt(uploadType);
        String pre = "local";
        switch (type) {
            case 2:
                pre = "qn";
                break;
            case 3:
                pre = "al";
                break;
            case 4:
                pre = "tx";
                break;
            default:
                break;
        }
        return systemConfigService.getValueByKey(pre+"UploadUrl");
    }
}
